package com.ipay.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ipay.dao.BankInfoDao;
import com.ipay.dao.CashiersDao;
import com.ipay.dao.MerchantInfoDao;
import com.ipay.dao.QRCodeDao;
import com.ipay.entity.BankInfoEntity;
import com.ipay.entity.CashiersEntity;
import com.ipay.entity.MerchantInfoEntity;
import com.ipay.entity.QRCodeEntity;
import com.ipay.service.BankInfoService;
import com.ipay.utils.Constant;
import com.ipay.utils.DateUtils;
import com.ipay.utils.RandomStringUtils;



@Transactional
@Service("bankInfoService")
public class BankInfoServiceImpl implements BankInfoService {
	@Autowired
	private BankInfoDao bankInfoDao;
	@Autowired
	private QRCodeDao codeDao;
	@Autowired
	private MerchantInfoDao merchantInfoDao;
	@Autowired
	private CashiersDao cashiersDao;

	@Override
	public BankInfoEntity queryObject(String id) {
		return bankInfoDao.queryObject(id);
	}

	@Override
	public List<BankInfoEntity> queryList(Map<String, Object> map) {
		return bankInfoDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return bankInfoDao.queryTotal(map);
	}
	
	@Transactional
	@Override
	public void save(BankInfoEntity bankInfo) {
		bankInfoDao.save(bankInfo);
		//添加银行卡后默认生成一个支付二维码
        StringBuffer qrurl = new StringBuffer();
        String merchantNo = bankInfo.getMerchantNo();// 商户号
		//下载时访问地址
		qrurl.append("merchant/userAgent");//http://www.***.cn/wechat/merchant/userAgent   Constant.DOMAINURL+
		qrurl.append("?merchantNo=" + merchantNo);
		
		//新增二维码
		QRCodeEntity qRCode = new QRCodeEntity();
		qRCode.setCodeName("主码");        //收款码名称
		qRCode.setMerchantId(bankInfo.getMerchantNo()); //商户编号
		
		//根据商户号查询商户表、再根据商户号、手机号、角色类型查询收款员表
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("merchantNo", merchantNo);
		//map.put("activateState", Constant.ActivateState.ACTIVATION.getValue());
		map.put("role", Constant.ROLE_SH);
		//CashiersEntity cashiers = cashiersDao.findCashierByParams(map);
		
		qRCode.setUrl(qrurl.toString()); //访问路径
		qRCode.setDeleteStatus(String.valueOf(Constant.DELETE_STATUS_NO));	//删除状态  0：未删除1：已删除 
		qRCode.setCodeStatus(Constant.CODE_STATUS_SD);		//二维码状态  0、系统生成 1、手动添加
		qRCode.setBankId(bankInfo.getId());//默认值
		
		List<CashiersEntity> list = cashiersDao.queryList(map);
		if(list.size() > 0){
			for(CashiersEntity cash : list){
				String date = DateUtils.format(new Date(), "yyyyMMdd");
		        String random = RandomStringUtils.randomNumeric(6);
		        String codeNo = date + random;
		        qRCode.setCodeNo(codeNo);       //收款码编号
				qRCode.setUserId(String.valueOf(cash.getId()));//收款员ID 
				codeDao.insertQrCode(qRCode);
			}
		}
	}

	@Override
	public int update(BankInfoEntity BankInfo) {
		return bankInfoDao.update(BankInfo);
		
	}

	@Override
	public int deleteBatch(String[] ids) {
		return bankInfoDao.deleteBatch(ids);
	}
	
	/**
	 * 根据商户号查询列表 格式化下拉
	 * 	[
	      { text: '微信', value: '1' },
	      { text: '支付宝', value: '2' }
	    ]
	 */
	@Override
	public List<BankInfoEntity> getBankInfoSelect(Map<String, Object> map) {
		List<BankInfoEntity> list = bankInfoDao.getBankInfoSelect(map);
		/*StringBuffer sb = new StringBuffer();
		if(list.size() > 0){
			sb.append("[");
			for(int i = 0;i < list.size();i++){
				sb.append("{");
				sb.append("text:"+"'"+list.get(i).getBankCard()+"'"+",");
				sb.append("value:"+"'"+list.get(i).getId()+"'");
				sb.append("}");
				if(i != list.size() - 1){
					sb.append(",");
				}
				
			}
			sb.append("]");
		}*/
		return list;
	}

	@Override
	public BankInfoEntity queryInfoByParams(Map<String, Object> map) {
		return bankInfoDao.queryInfoByParams(map);
	}

}
